package br.com.social.mobile.util;

import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.io.File;

import javax.imageio.ImageIO;

import br.com.social.mobile.dao.ParameterDAO;
import br.com.social.mobile.entities.Parameter;

public class ImageWatermark {

	public static void mergeImage(String idProfile) {
		try {
				ParameterDAO dao = new ParameterDAO();
				Parameter parameter = dao.findByParamName(IConstants.PARAMETER.PATH_SAVE_IMAGE);
				String path = parameter.getDsParamValue();

				// load source images
				BufferedImage image = ImageIO.read(new File(path, idProfile+ ".gif"));
				BufferedImage overlay = ImageIO.read(new File(path, "frame_marker.png"));
				
				// create the new image, canvas size is the max. of both image sizes
				int w = Math.max(overlay.getWidth(), overlay.getWidth());
				int h = Math.max(overlay.getHeight(), overlay.getHeight());
				BufferedImage combined = new BufferedImage(w, h, BufferedImage.TYPE_INT_ARGB);

				// paint both images, preserving the alpha channels
				Graphics g = combined.getGraphics();
				g.drawImage(image, 3, 3, 73, 73, null);
				g.drawImage(overlay, 0, 0, null);

				// Save as new image
				ImageIO.write(combined, "PNG", new File(path, idProfile+ "_marker.png"));
				
		} catch (Exception e) {
			System.out.println(e);
		}
		
	}
	

	public static void saveImages(BufferedImage uploaded, String nameImg) {
		try {
				ParameterDAO dao = new ParameterDAO();
				Parameter parameter = dao.findByParamName(IConstants.PARAMETER.PATH_SAVE_IMAGE);
				String path = parameter.getDsParamValue();

				// load source images
				BufferedImage image = uploaded;
				BufferedImage overlay = ImageIO.read(new File(path, "frame_marker.png"));
				
				// create the new image, canvas size is the max. of both image sizes
				int w = Math.max(overlay.getWidth(), overlay.getWidth());
				int h = Math.max(overlay.getHeight(), overlay.getHeight());
				BufferedImage combined = new BufferedImage(w, h, BufferedImage.TYPE_INT_ARGB);
				BufferedImage origem = new BufferedImage(w, h, BufferedImage.TYPE_INT_ARGB);

				// paint both images, preserving the alpha channels
				Graphics g = combined.getGraphics();
				g.drawImage(image, 3, 3, 73, 73, null);
				g.drawImage(overlay, 0, 0, null);
				
				Graphics o = origem.getGraphics();
				o.drawImage(image, 3, 3, 73, 73, null);

				// Save as new image
				ImageIO.write(combined, "PNG", new File(path, nameImg+ "_marker.png"));
				ImageIO.write(origem, "PNG", new File(path, nameImg+ ".png"));
				
		} catch (Exception e) {
			System.out.println(e);
		}
		
	}	
	public static void main(String[] args) {
		ImageWatermark.mergeImage("29009");
	}
}
